/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

package team.mai.antexpress.dao;


import com.sun.istack.internal.Nullable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import team.mai.antexpress.entities.DesignationEntity;
import team.mai.antexpress.services.DesignationService;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("Duplicates")
@Repository
public class DesignationDAO {
	@Autowired
	SessionFactory sessionFactory;
	public Integer insertDesignation(DesignationEntity dsgt) {
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			Serializable save = session.save(dsgt);
			transaction.commit();
			return (Integer) save;
		} catch (Exception ex) {
			ex.printStackTrace();
			return DesignationService.ERROR;
		}


	}

	public boolean deleteDesignation(DesignationEntity dsgt) {
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			session.delete(dsgt);
			transaction.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public boolean hasUserInDesignation(int dsgtId) {
		try (Session session = sessionFactory.openSession()) {
			DesignationEntity designationEntity = session.get(DesignationEntity.class, dsgtId);
			Query query = session.createQuery("FROM UserEntity WHERE designationId=:dsgt");
			query.setParameter("dsgt", dsgtId);
			return query.list().size() > 0;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean deleteUsersInDepartment(int dsgtId) {
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			Query query = session.createQuery("DELETE FROM UserEntity WHERE designationId=:dsgt");
			query.setParameter("dsgt", dsgtId);
			query.executeUpdate();
			transaction.commit();
			return true;
		} catch (Exception e){
			e.printStackTrace();
			return false;
		}
	}
	public boolean deleteDesignation(int id) {
		DesignationEntity designationEntity = sessionFactory.openSession().get(DesignationEntity.class, id);
		return deleteDesignation(designationEntity);
	}

	public boolean deleteDesignationsById(List<Integer> ids) {
		boolean status = true;
		for (Integer id : ids) {
			if (!deleteDesignation(id)) {
				status = false;
				break;
			}
		}
		return status;
	}
	public boolean deleteDesignationsByEntity(List<DesignationEntity> dsgts) {
		boolean status = true;
		for (DesignationEntity dsgt : dsgts) {
			if (!deleteDesignation(dsgt)) {
				status = false;
				break;
			}
		}
		return status;
	}
	public boolean updateDesignation(DesignationEntity dsgt) {
		try (Session session = sessionFactory.openSession()) {
			Transaction transaction = session.beginTransaction();
			session.update(dsgt);
			transaction.commit();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public List<DesignationEntity> getAllDesignations() {
		try (Session session = sessionFactory.openSession()) {
			Query<DesignationEntity> query = session.createQuery("FROM DesignationEntity");
			return query.list();
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<>();
		}
	}
	@Nullable
	public DesignationEntity getDesignationById(int dsgtId) {
		try (Session session = sessionFactory.openSession()) {
			return session.get(DesignationEntity.class, dsgtId);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public boolean ifExistsDesignation(String dsgtName) {
		try (Session session = sessionFactory.openSession()) {
			Query query = session.createQuery("FROM DesignationEntity WHERE name=:dsgtName");
			query.setParameter("dsgtName", dsgtName);
			List list = query.list();
			return !list.isEmpty();
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
	}
	public boolean ifExistsDesignation(String except, String dsgtName) {
		try (Session session = sessionFactory.openSession()) {
			Query query = session.createQuery("FROM DesignationEntity WHERE name=:dsgtName");
			query.setParameter("dsgtName", dsgtName);
			List list = query.list();
			if (list.isEmpty()) {
				return false;
			} else {
				if (((DesignationEntity) list.get(0)).getName().equals(except)) {
					return false;
				} else {
					return true;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
	}

}
